package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"month/api/request"
	"month/global"
	"month/internal/dao"
	"month/internal/model"
	"month/pkg"
	"time"
)

func AddVideo(c *gin.Context) {
	file, _ := c.FormFile("file")
	if file == nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "文件不能为空",
			"data": nil,
		})
	}
	dst := "D:\\goWork\\src\\month\\status\\" + file.Filename
	// 上传文件至指定的完整文件路径
	fileName := fmt.Sprintf("https://%v%v", time.Now().Format("20060102150405"), file.Filename)
	c.SaveUploadedFile(file, dst)
	pkg.UploadFile(dst, fileName)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "上传成功",
		"data": fileName,
	})
}

func CreateVideo(c *gin.Context) {
	var req request.CreateVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneByFields(&model.Videos{VName: req.VName}, &video)
	if video.Id != 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "文件已存在",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneById(c.GetUint("userId"), &user)

	var FUser model.Users
	dao.GetOneById(uint(req.FId), &FUser)
	if user.UType != 1 {
		videos := model.Videos{
			VName:    req.VName,
			VUrl:     req.VUrl,
			UId:      uint64(c.GetUint("userId")),
			UMobile:  user.Mobile,
			UNumber:  user.WorkNumber,
			FId:      req.FId,
			UFounder: FUser.Mobile,
			FNumber:  FUser.WorkNumber,
			RStatus:  3,
			CartOne:  req.CartOne,
			CartTwo:  req.CartTwo,
			Image:    req.Image,
			Keywords: req.Keywords,
			VInfo:    req.VInfo,
			VType:    req.VType,
		}
		if !dao.Create(&videos) {
			c.JSON(200, gin.H{
				"code": 10002,
				"msg":  "发布失败",
				"data": nil,
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "发布成功",
			"data": videos.VName,
		})
	} else {
		videos := model.Videos{
			VName:    req.VName,
			VUrl:     req.VUrl,
			UId:      uint64(c.GetUint("userId")),
			UMobile:  user.Mobile,
			UNumber:  user.WorkNumber,
			FId:      req.FId,
			UFounder: FUser.Mobile,
			FNumber:  FUser.WorkNumber,
			CartOne:  req.CartOne,
			CartTwo:  req.CartTwo,
			Image:    req.Image,
			Keywords: req.Keywords,
			VInfo:    req.VInfo,
			VType:    req.VType,
		}
		if !dao.Create(&videos) {
			c.JSON(200, gin.H{
				"code": 10002,
				"msg":  "发布失败",
				"data": nil,
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "发布成功",
			"data": videos.VName,
		})
	}
}

func UpdatedVideo(c *gin.Context) {
	var req request.UpdatedVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneById(uint(req.Id), &video)
	if video.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "没有此内容",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneById(c.GetUint("userId"), &user)

	videos := model.Videos{
		Id:       req.Id,
		VName:    req.VName,
		VUrl:     req.VUrl,
		UId:      uint64(c.GetUint("userId")),
		UMobile:  user.Mobile,
		UNumber:  user.WorkNumber,
		CartOne:  req.CartOne,
		CartTwo:  req.CartTwo,
		Image:    req.Image,
		Keywords: req.Keywords,
		VInfo:    req.VInfo,
		VType:    req.VType,
		VStatus:  req.VStatus,
	}
	if !dao.Update(&videos) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "更新失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "更新成功",
		"data": videos,
	})
}
func UpdateVideo(c *gin.Context) {
	var req request.UpdateVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneById(uint(req.Id), &video)
	if video.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "没有此内容",
			"data": nil,
		})
		return
	}
	var user model.Users
	dao.GetOneById(c.GetUint("userId"), &user)
	videos := model.Videos{
		Id:        req.Id,
		RId:       uint64(c.GetUint("userId")),
		UReviewer: user.Mobile,
		RNumber:   user.WorkNumber,
		RStatus:   req.RStatus,
	}
	if !dao.Update(&videos) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "审核失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "审核成功",
		"data": video,
	})
}

func DeleteVideo(c *gin.Context) {
	var req request.DeleteVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneById(uint(req.Id), &video)
	if video.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "没有此内容",
			"data": nil,
		})
		return
	}
	videos := model.Videos{Id: req.Id}
	if !dao.Delete(&videos) {
		c.JSON(200, gin.H{
			"code": 10002,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": nil,
	})
}

func ListVideo(c *gin.Context) {
	var videos []model.Videos
	err := global.DB.Debug().Find(&videos).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": videos,
	})
}

func QueryVideo(c *gin.Context) {
	var req request.QueryVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video model.Videos
	dao.GetOneById(uint(req.Id), &video)
	if video.Id == 0 {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "没有此内容",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func StatusVideo(c *gin.Context) {
	var req request.StatusVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	dao.GetListByFields(&model.Videos{RStatus: uint64(req.RStatus)}, &video)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func CastOneVideo(c *gin.Context) {
	var req request.CastOneVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	dao.GetListByFields(&model.Videos{CartOne: req.CartOne}, &video)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func CastTwoVideo(c *gin.Context) {
	var req request.CastTwoVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	dao.GetListByFields(&model.Videos{CartTwo: req.CartTwo}, &video)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func NameVideo(c *gin.Context) {
	var req request.NameVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	err := global.DB.Where("v_name like ?", "%"+req.VName+"%").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func FounderNumVideo(c *gin.Context) {
	var req request.FounderNumVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	err := global.DB.Where("f_number like ?", "%"+req.FNumber+"%").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func UserNumVideo(c *gin.Context) {
	var req request.UserNumVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	err := global.DB.Where("u_number like ?", "%"+req.UNumber+"%").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func ReviewerNumVideo(c *gin.Context) {
	var req request.ReviewerNumVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	err := global.DB.Where("r_number like ?", "%"+req.RNumber+"%").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}

func KeywordsVideo(c *gin.Context) {
	var req request.KeywordsVideoReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": err.Error(),
		})
		return
	}
	var video []model.Videos
	err := global.DB.Where("keywords like ?", "%"+req.Keywords+"%").Find(&video).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 10001,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": video,
	})
}
